package org.iwidget.desktop.model;
import org.iwidget.desktop.core.IwidgetRepository;
import java.io.ByteArrayInputStream;
import java.net.URL;
import org.w3c.dom.*;
//import javax.media.*;
import sun.applet.AppletAudioClip;

/**
 *
 * @author Muhammad Hakim A
 */



public class ElementSound extends Element //implements ControllerListener
{

    public ElementSound()
    {
        data = null;
        name = "Sound";
    }

    public ElementSound(WidgetElement widget)
    {
        super(widget);
        data = null;
        name = "Sound";
    }

    public String getData()
    {
        return data;
    }

    public void setData(String s)
    {
        if(data == null)
            data = s;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String s)
    {
        name = s;
    }

    public void play()
    {
        // old implementation, using Sun applet library
        createClipIfRequired();
        c.stop();
        c.play();
        // new: using jmf library, next time will be using fmj
//        if (player != null)
//        {
//            player.stop();
//            player.start();
//        }
    }

    public void stop()
    {
        // old version
        if(c != null)
            c.stop();
        // new version
//        if (player != null)
//        {
//            player.stop();
//        }
    }

    public void loop()
    {
        createClipIfRequired();
        //c.stop();
        //c.loop();
        //isLoop = true;
    }

    private void createClipIfRequired()
    {
        System.out.println("audio data : "+data+" widgetname: "+widget.getName());
        if(c == null)
            try
            {
                if(data.indexOf("://") > 0)
                {
                    // old
                    c = new AppletAudioClip(new URL(data));
                    // new JMF
                    //player = Manager.createPlayer(new URL(data));
                } else
                {
                    byte bytes[] = IwidgetRepository.getInstance().getObject(widget.getName(), data);
                    ByteArrayInputStream in = new ByteArrayInputStream(bytes);
                    c = new AppletAudioClip(bytes);
                }
            }
            catch(Exception e)
            {
                e.printStackTrace();
                widget.getParent().getDebugFrame().addText("SYSTEM:Error:Invalid sounds file:" + widget.getName() + ":" + data);
            }
    }

    public void parse(Node windowNode)
        throws Exception
    {
        NamedNodeMap map = windowNode.getAttributes();
        for(int iCtr = 0; iCtr < map.getLength(); iCtr++)
        {
            Node attr = map.item(iCtr);
            parseHelper(attr.getNodeName(), attr.getNodeValue());
        }

        NodeList nodes = windowNode.getChildNodes();
        for(int iCtr = 0; iCtr < nodes.getLength(); iCtr++)
        {
            Node childNode = nodes.item(iCtr);
            String nodeName = childNode.getNodeName();
            String nodeValue = childNode.getNodeValue();
            if(childNode.getFirstChild() != null)
                parseHelper(nodeName, childNode.getFirstChild().getNodeValue());
        }

    }

    private void parseHelper(String nodeName, String value)
    {
        if(nodeName.equals("data"))
            setData(value);
        else
        if(nodeName.equals("name"))
            setName(value);
    }

//public synchronized void controllerUpdate(ControllerEvent event) {
//    if (player == null)
//        return;
//	
//    if (event instanceof RealizeCompleteEvent) {
//	// complete
//    }else if (event instanceof CachingControlEvent) {
//        if (player.getState() > Controller.Realizing)
//            return;
//    } else if (event instanceof EndOfMediaEvent) {
//        if (isLoop){
//            player.setMediaTime(new Time(0));
//            player.start();
//        }else{
//            player.close();
//        }
//    } else if (event instanceof ControllerErrorEvent) {
//        player = null;
//	Fatal(((ControllerErrorEvent)event).getMessage());
//   } else if (event instanceof ControllerClosedEvent) {
//	// do nothing, controller tidak dipakai    
//   }
//}

public void Fatal (String s) {
    System.err.println("FATAL ERROR: " + s);
    throw new Error(s); // Invoke the uncaught exception
                        // handler System.exit() is another
                        // choice.
}

    //private boolean isLoop = false;
    private String data;
    private String name;
    //private Player player = null;
    //private URL url = null;
    private AppletAudioClip c;
}